package 堆栈与队列;

import java.util.Comparator;
import java.util.PriorityQueue;

public class 滑动窗口最大值 {
    public int[] maxSlidingWindow(int[] nums, int k) {
        if (nums.length == 0) {
            return nums;
        }
        int[] result = new int[nums.length - k + 1];
        PriorityQueue<Integer> queue = new PriorityQueue<>(Comparator.reverseOrder());

        for (int i = 0; i < k; i++) {
            int num = nums[i];
            queue.add(num);
        }
        result[0] = queue.peek();
        //    开始滑动窗口
        for (int i = k; i < nums.length; i++) {
            int num1 = nums[i - k];
            int num2 = nums[i];
            queue.remove(num1);
            queue.add(num2);
            result[i - k + 1] = queue.peek();
        }
        return result;
    }

}
